package com.dingreading.cloud.admin.controller;

import com.alibaba.fastjson.JSONObject;
import com.dingreading.cloud.admin.entity.Corporations;
import com.dingreading.cloud.admin.service.CorporationsService;
import com.dingreading.cloud.common.base.R;
import com.dingreading.cloud.common.util.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/system/corporations")
@Api(tags = "总部-加盟商信息管理")
public class CorporationsController {

    @Resource
    private CorporationsService corporationsService;


    @ApiOperation(value = "分页列表")
    @PostMapping(value = "/pageList")
    public R<Object> pageList(
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "分页公共类") PageUtil pageUtil,
            @ApiParam(value = "公司名") @RequestParam(value = "keyword", required = false) String keyword,
            @ApiParam(value = "是否上传合同 1.是 0.否") @RequestParam(value = "contractStatus", required = false) Integer contractStatus
    ) {
        PageDto<JSONObject> data = corporationsService.pageList(pageUtil, keyword, contractStatus);
        return R.ok(data);
    }

    @ApiOperation(value = "根据公司uid获取信息")
    @PostMapping(value = "/getBean", produces = "application/json; charset=UTF-8")
    public R<Object> getBean(
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "公司uid") @RequestParam(value = "corpUid") String corpUid
    ) {
        Corporations data = corporationsService.getByUid(corpUid);
        return R.ok(data);
    }

    @ApiOperation(value = "根据全部公司")
    @PostMapping(value = "/getAll", produces = "application/json; charset=UTF-8")
    public R<Object> getAll(
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "公司名") @RequestParam(value = "keyword", required = false) String keyword
    ) {
        List<Corporations> list = corporationsService.getByKeyword(keyword);
        return R.ok(list);
    }

    @Transactional(rollbackFor = DescribeException.class)
    @ApiOperation(value = "创建/更新公司信息")
    @PostMapping(value = "/newBean", produces = "application/json; charset=UTF-8")
    public R<Object> newBean(
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "id") @RequestParam(value = "id", required = false) Long id,
            @ApiParam(value = "加盟商uid") @RequestParam(value = "uid", required = false) String uid,
            @ApiParam(value = "公司名") @RequestParam(value = "corpName") String corpName,
            @ApiParam(value = "省") @RequestParam(value = "province", required = false) String province,
            @ApiParam(value = "市") @RequestParam(value = "city", required = false) String city,
            @ApiParam(value = "区县") @RequestParam(value = "county", required = false) String county,
            @ApiParam(value = "投资人姓名") @RequestParam(value = "investorName", required = false) String investorName,
            @ApiParam(value = "投资人电话") @RequestParam(value = "investorMob", required = false) String investorMob,
            @ApiParam(value = "联系人姓名") @RequestParam(value = "contactName", required = false) String contactName,
            @ApiParam(value = "联系人电话") @RequestParam(value = "contactMob", required = false) String contactMob,
            @ApiParam(value = "crm录入时间") @RequestParam(value = "crmDate", required = false) String crmDate
    ) throws Exception {
        if (StringUtils.isNotBlank(investorMob) && !RegularUtil.isValidPhoneNumber(investorMob))
            return R.fail("投资人电话错误");
        if (StringUtils.isNotBlank(contactMob) && !RegularUtil.isValidPhoneNumber(contactMob))
            return R.fail("联系人电话错误");

        Corporations data = null;
        if (null == id || id <= 0) {
            if (StringUtils.isNotBlank(uid)) {
                boolean b = corporationsService.existUid(uid);
                if (b)
                    return R.fail("加盟商uid重复，请修改后提交");
            } else {
                SimpleDateFormat format = new SimpleDateFormat("yyMM");
                String yyMm = format.format(new Date());
                // 获取最新的公司uid
                Corporations lastData = corporationsService.getLastUid(yyMm);
                if (null == lastData) {
                    uid = yyMm + "01";
                } else {
                    String lastUid = lastData.getUid();
                    // 在最后一次出现"yyMm"后获取其之后的字符串
                    String lastString = StringUtils.substringAfterLast(lastUid, yyMm);
                    if (StringUtils.isBlank(lastString))
                        return R.fail("获取截取后的公司uid失败");
                    int num = Integer.parseInt(lastString) + 1;
                    // 数字最后一位是4则跳过
                    num = TextUtils.dealLastFour(num);
                    if (num < 10) {
                        uid = yyMm + "0" + num;
                    } else {
                        uid = yyMm + num;
                    }
                }
            }
            data = new Corporations();
            data.setUid(uid);
        } else {
            data = corporationsService.getById(id);
            if (null == data) return R.fail("公司不存在");
        }
        data.setCorpName(corpName);
        data.setProvince(StringUtils.isNotBlank(province) ? province : "");
        data.setCity(StringUtils.isNotBlank(city) ? city : "");
        data.setCounty(StringUtils.isNotBlank(county) ? county : "");
        data.setInvestorName(investorName);
        data.setInvestorMob(investorMob);
        data.setContactName(contactName);
        data.setContactMob(contactMob);
        data.setCrmDate(StringUtils.isNotBlank(crmDate) ? DateUtil.parseDate(crmDate) : null);
        corporationsService.saveOrUpdate(data);
        return R.ok();
    }

}